package com.artron.mediaartron.ui.fragment.center;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.entity.Response;
import com.artron.baselib.utils.ToastUtil;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.config.AppProfile;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.net.api.HostApi;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-08.
 */

public class FeedbackFragment extends BaseStaticFragment {

    @BindView(R.id.FeedbackFragment_ll_container)
    protected LinearLayout mLlContainer;
    @BindView(R.id.FeedbackFragment_et_content)
    protected EditText mEtContent;
    @BindView(R.id.FeedbackFragment_et_contact)
    protected EditText mEtContact;

    public static FeedbackFragment newInstance() {
        Bundle args = new Bundle();
        FeedbackFragment fragment = new FeedbackFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_feedback;
    }

    @Override
    protected void initView() {
        mLlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIUtils.hiddenKeyboard(getView());
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_send) {

            String contact = mEtContact.getText().toString();
            String content = mEtContent.getText().toString();
            if (TextUtils.isEmpty(contact)) {
                ToastUtil.show("请填写联系方式");
                return false;
            }

            if (TextUtils.isEmpty(content)) {
                ToastUtil.show("请填写反馈内容");
                return false;
            }
            String phoneName = android.os.Build.MODEL ;
            String phoneVersion = android.os.Build.VERSION.RELEASE;



            String passId = AppProfile.getUserInfo().getPassId();
            HostApi api = RetrofitHelper.getHostApi();
            RetrofitHelper.subscript(api.saveFeedback(passId, content, contact, "1.0", phoneName, phoneVersion),
                    new Action1<Response>() {
                        @Override
                        public void call(Response response) {
                            UIUtils.showSnackbar(getView(), response.getMessage());
                            ToastUtil.show(response.getMessage());
                            getActivity().onBackPressed();
                        }
                    });
        }
        return super.onOptionsItemSelected(item);
    }
}
